
    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <title>String、StringBuffer、StringBuilder区别 - 学习卡片</title>
      <style>
        body { font-family: sans-serif; background-color: #f0f8ff; color: #333; display: flex; flex-direction: column; align-items: center; padding: 50px 20px; }
        .header h1 { font-size: 32px; }
        .grid-container { display: grid; grid-template-columns: repeat(3, 1fr); gap: 28px; width: 100%; max-width: 1200px; }
        .card-container { perspective: 1200px; cursor: pointer; height: 250px; }
        .card { width: 100%; height: 100%; position: relative; transform-style: preserve-3d; transition: transform 0.7s; border-radius: 16px; box-shadow: 0 4px 16px rgba(0,0,0,0.08); }
        .card-container.flipped .card { transform: rotateY(180deg); }
        .card-face { position: absolute; width: 100%; height: 100%; backface-visibility: hidden; display: flex; flex-direction: column; box-sizing: border-box; border-radius: 16px; background-color: #fff; padding: 24px; }
        .card-back { background-color: #f0fff4; transform: rotateY(180deg); justify-content: space-between; }
        .card-category { font-size: 14px; color: #0052d9; margin-bottom: 8px; font-weight: 500; }
        .card-question { font-size: 20px; font-weight: 500; flex-grow: 1; display: flex; align-items: center; justify-content: center; text-align: center; }
        .card-answer-wrapper { flex-grow: 1; overflow-y: auto; }
        .card-answer { font-size: 15px; line-height: 1.7; }
        .card-footer { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 16px; margin-top: 16px; }
        .card-source { font-size: 13px; color: #8a919f; border-top: 1px solid #f0f0f0; padding-top: 12px; margin-top: 12px; }
      </style>
    </head>
    <body>
      <div class="header">
        <h1>String、StringBuffer、StringBuilder区别 - 学习卡片</h1>
      </div>
      <div class="grid-container">
        
    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">特性</div>
          <div class="card-question">Java中String类的核心特性是什么？为什么说它是“不可变”的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">特性</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">String是一个不可变类。一旦String对象被创建，其内容就不可改变。任何对String对象的操作，例如修改，实际上都会生成一个新的String对象，而原始对象保持不变。</div>
          </div>
          <div class="card-source">来源: 1. String</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">为什么在频繁修改字符串的场景下，使用String类会导致性能下降？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">由于String对象的不可变性，每次修改操作都会创建一个新的对象。在频繁操作中，这会导致大量新对象的创建，从而影响系统性能。</div>
          </div>
          <div class="card-source">来源: 1. String > 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">对比</div>
          <div class="card-question">StringBuffer和StringBuilder最核心的区别是什么？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">对比</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">最核心的区别在于线程安全性。StringBuffer是线程安全的，其方法使用了synchronized关键字进行同步。而StringBuilder是非线程安全的，但因此在单线程环境中有更高的性能。</div>
          </div>
          <div class="card-source">来源: 3. StringBuilder</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">场景</div>
          <div class="card-question">在什么情况下应该优先选择使用StringBuffer？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">场景</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">当需要在多线程环境下对字符串进行频繁修改时，应该优先选择StringBuffer。因为它是线程安全的，可以保证多个线程安全地操作同一个对象。</div>
          </div>
          <div class="card-source">来源: 2. StringBuffer > 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">场景</div>
          <div class="card-question">在何种场景下，StringBuilder是处理字符串操作的首选？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">场景</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">在单线程环境中进行大量或频繁的字符串拼接、修改操作时，StringBuilder是首选。因为它没有同步开销，性能是三者中最高的。</div>
          </div>
          <div class="card-source">来源: 3. StringBuilder > 使用场景</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">机制</div>
          <div class="card-question">StringBuffer是如何实现其线程安全特性的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">机制</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">StringBuffer通过在其方法上使用`synchronized`关键字来实现线程安全，这确保了在多线程环境下修改字符串时操作的原子性和安全性。</div>
          </div>
          <div class="card-source">来源: 2. StringBuffer</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">对比</div>
          <div class="card-question">请根据文档总结，从性能角度看，这三个类的排序是怎样的？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">对比</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">性能从高到低排序为：StringBuilder > StringBuffer > String。StringBuilder性能最高，因为它无需同步；StringBuffer因同步机制有一定开销；String在频繁修改时因创建新对象而性能相对最低。</div>
          </div>
          <div class="card-source">来源: 4. 对比总结</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">场景</div>
          <div class="card-question">除了用于少量字符串操作，String的不可变性使其还特别适用于哪种编程场景？</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">场景</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">由于其不可变性，String非常适合用作键值对（例如在哈希表中）的键（key），因为其内容不会改变，保证了键的稳定性。</div>
          </div>
          <div class="card-source">来源: 5. 选择的建议</div>
        </div>
      </div>
    </div>

    <div class="card-container" onclick="this.classList.toggle('flipped');">
      <div class="card">
        <div class="card-face card-front">
          <div class="card-category">对比</div>
          <div class="card-question">根据总结表格，请分别描述String, StringBuffer, 和 StringBuilder在可变性和线程安全性上的特点。</div>
          <div class="card-footer">点击卡片查看答案</div>
        </div>
        <div class="card-face card-back">
          <div class="card-category">对比</div>
          <div class="card-answer-wrapper">
            <div class="card-answer">String是不可变的，并且是线程安全的。StringBuffer是可变的，也是线程安全的。StringBuilder是可变的，但非线程安全。</div>
          </div>
          <div class="card-source">来源: 4. 对比总结</div>
        </div>
      </div>
    </div>

      </div>
    </body>
    </html>
